#!/bin/bash
# Entry point do .app bundle. Invocado pelo macOS sem terminal visível.
set +e
APP_DIR="$(cd "$(dirname "$0")/.." && pwd)"
RES="$APP_DIR/Resources"

# remove quarentena recursiva (Gatekeeper pode ter marcado tudo)
xattr -dr com.apple.quarantine "$APP_DIR/.." 2>/dev/null || true

# escolhe binário pela arquitetura
ARCH=$(uname -m)
if [ "$ARCH" = "arm64" ]; then
    AGENT="$RES/gobackup-agent-arm64"
else
    AGENT="$RES/gobackup-agent-amd64"
fi

LOG="/tmp/gobackup-installer.log"
echo "[$(date)] iniciando instalador (arch=$ARCH)" >>"$LOG"

if ! "$AGENT" install --token-file "$RES/token.dat" >>"$LOG" 2>&1; then
    osascript -e 'display dialog "Falha ao iniciar o instalador Go Backup. Detalhes em /tmp/gobackup-installer.log" buttons {"OK"} default button "OK" with icon stop' >/dev/null 2>&1
    exit 1
fi
